/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pdfManager;

import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Graphics2D;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.swing.JPanel;

/**
 *
 * @author Allan
 */
public abstract class PanelToPDF
{
    public static boolean createPdfFromPanel(JPanel documentPanel, String path) throws DocumentException, FileNotFoundException, Exception
    {
        boolean result;
        result = false;
        
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight();

        PdfTemplate resultsPanelPdfTemplate = cb.createTemplate(width, height);
        Graphics2D g2d2 = new PdfGraphics2D(resultsPanelPdfTemplate, width, height);
        documentPanel.paint(g2d2);
        g2d2.dispose();
        cb.addTemplate(resultsPanelPdfTemplate, 50, -70);

        document.close(); 
        
        result = true;
        
        return result;
    }
}
